package cn.yeyiu.util;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.yeyiu.util.bean.MangaDetail;
import cn.yeyiu.util.bean.VideoInfo;

import java.util.ArrayList;
import java.util.List;

public class VideoUtil {

    private static List<String> getAnimeSerials(String str) {
        String byUrl = NetUtil.getByUrl(str);
        if (byUrl != null) {
            return JSONArray.parseArray(byUrl, String.class);
        }
        AndroidUtil.netErrorToast();
        return null;
    }

    private static void postAnimeSerials(String str, MutableLiveData<List<String>> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getAnimeSerials(str));
        });
    }

    // 获取单个video
    private static VideoInfo getVideo(String str) {
        String strObj = NetUtil.getByUrl(str);
        if (strObj != null) {
            return JSONObject.parseObject(strObj, VideoInfo.class);
        }
        AndroidUtil.netErrorToast();
        return null;
    }

    private static void postVideo(String url, MutableLiveData<VideoInfo> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getVideo(url));
        });
    }

    // 重复获取video构成 video list （一般使用获取随机）
    private static List<VideoInfo> getCountVideos(String url, int n) {
        List<VideoInfo> list = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
            VideoInfo video = getVideo(url);
            if (video == null) {
                AndroidUtil.netErrorToast();
                return null;
            }
            list.add(video);
        }
        return list;
    }

    private static void postCountVideos(String url, int n, MutableLiveData<List<VideoInfo>> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getCountVideos(url, n));
        });
    }

    // 获取video list
    private static List<VideoInfo> getVideos(String url) {
        String byUrl = NetUtil.getByUrl(url);
        if (byUrl != null) {
            return JSONArray.parseArray(byUrl, VideoInfo.class);
        }
        AndroidUtil.netErrorToast();
        return null;
    }

    private static void postVideos(String url, MutableLiveData<List<VideoInfo>> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getVideos(url));
        });
    }

    // 获取单个manga
    private static MangaDetail getManga(String url) {
        String byUrl = NetUtil.getByUrl(url);
        if (byUrl != null) {
            return JSONObject.parseObject(byUrl, MangaDetail.class);
        }
        AndroidUtil.netErrorToast();
        return null;
    }

    private static void postManga(String url, MutableLiveData<MangaDetail> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getManga(url));
        });
    }

    // 获取manga list
    private static List<MangaDetail> getMangas(String url) {
        String byUrl = NetUtil.getByUrl(url);
        if (byUrl != null) {
            return JSONArray.parseArray(byUrl, MangaDetail.class);
        }
        AndroidUtil.netErrorToast();
        return null;
    }

    private static void postMangas(String str, MutableLiveData<List<MangaDetail>> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getMangas(str));
        });
    }

    private static String buildRandomVideoUrl(String type) {
        String url = NetConfig.getHost() + "/%s/%s_random";
        String type2 = "anime".equals(type) ? type : "other";
        return String.format(url, type2, type);
    }

    // --------------------------------------
    //         对外接口
    // --------------------------------------

    // manga
    public static List<MangaDetail> getMangaList() {
        return getMangas(NetConfig.getHost() + "/manga/list");
    }

    public static void postMangaList(MutableLiveData<List<MangaDetail>> mutableLiveData) {
        postMangas(NetConfig.getHost() + "/manga/list", mutableLiveData);
    }

    // anime
    public static List<String> getAnimeSerialList(String year) {
        return getAnimeSerials(NetConfig.getHost() + "/anime/anime_ctgs?year=" + year);
    }

    public static List<VideoInfo> getSerialAnimeList(String serial) {
        return getVideos(NetConfig.getHost() + "/anime/animes?serial=" + serial);
    }

    public static void postAnimeSerialList(String year, MutableLiveData<List<String>> mutableLiveData) {
        ThreadUtil.run(() -> {
            mutableLiveData.postValue(getAnimeSerials(NetConfig.getHost() + "/anime/anime_ctgs?year=" + year));
        });
    }

    public static void postSerialAnimeList(String serial, MutableLiveData<List<VideoInfo>> mutableLiveData) {
        postVideos(NetConfig.getHost() + "/anime/animes?serial=" + serial, mutableLiveData);
    }

    // other video
    public static List<VideoInfo> getOtherVideoList(String str) {
        return getVideos(String.format(NetConfig.getHost() + "/other/%ss", str));
    }

    public static List<VideoInfo> searchVideoList(String key, String type) {
        String url = NetConfig.getHost() + "/%s/%s_search?key=%s";
        String type2 = "anime".equals(type) ? type : "other";
        return getVideos(String.format(url, type2, type, key));
    }

    public static void postOtherVideoList(String str, MutableLiveData<List<VideoInfo>> mutableLiveData) {
        postVideos(String.format(NetConfig.getHost() + "/other/%ss", str), mutableLiveData);
    }

    public static void searchVideoList(String key, String type, MutableLiveData<List<VideoInfo>> mutableLiveData) {
        String url = NetConfig.getHost() + "/%s/%s_search?key=%s";
        String type2 = "anime".equals(type) ? type : "other";
        postVideos(String.format(url, type2, type, key), mutableLiveData);
    }

    public static VideoInfo getRandomVideo(String type) {
        return getVideo(buildRandomVideoUrl(type));
    }

    public static List<VideoInfo> getRandomCountVideos(String type, int n) {
        return getCountVideos(buildRandomVideoUrl(type), n);
    }

    public static void postRandomVideo(String type, MutableLiveData<VideoInfo> mutableLiveData) {
        postVideo(buildRandomVideoUrl(type), mutableLiveData);
    }

    public static void postRandomCountVideos(String type, int n, MutableLiveData<List<VideoInfo>> videoListLd) {
        postCountVideos(buildRandomVideoUrl(type), n, videoListLd);
    }

    public static List<VideoInfo> getGuessVideoList() {
        return getRandomCountVideos("live2d", 3);
    }

    public static List<VideoInfo> getAnyRandomVideoList() {
        List<VideoInfo> list = new ArrayList<>();
        list.addAll(getCountVideos("anime", 6));
        list.addAll(getCountVideos("normal", 3));
        return list;
    }

    public static void postGuessVideoList(List<VideoInfo> list) {
        ThreadUtil.run(() -> {
            list.addAll(getCountVideos("live2d", 3));
        });
    }

    public static void postAnyRandomVideoList(MutableLiveData<List<VideoInfo>> videoListLd) {
        ThreadUtil.run(() -> {
            videoListLd.postValue(getAnyRandomVideoList());
        });
    }
}
